package com.test.utils.search;

import java.io.Serializable;

public class SearchUtils {

	public static SearchResultIndex calculateIndexes(final int resultListSize, final int pageNumber, final int itemsPerPage) {
		final SearchResultIndex result = new SearchResultIndex();
		if (resultListSize <= 0) {
			return result;
		}
		if (pageNumber <= 0 || itemsPerPage <= 0) {
			result.startIndex = 0;
			result.endIndex = resultListSize;
			return result;
		}
		if (((pageNumber - 1) * itemsPerPage) >= resultListSize) {
			return result;
		}
		result.startIndex = (pageNumber - 1) * itemsPerPage;
		if (pageNumber * itemsPerPage < resultListSize) {
			result.endIndex = (pageNumber * itemsPerPage);
		} else {
			result.endIndex = resultListSize;
		}
		return result;
	}

	public static class SearchResultIndex implements Serializable {

		/**
		 * Generated serial version UID.
		 */
		private static final long serialVersionUID = 7729201411079404405L;

		private int startIndex = -1;

		private int endIndex;

		public int getStartIndex() {
			return startIndex;
		}

		public void setStartIndex(int startIndex) {
			this.startIndex = startIndex;
		}

		public int getEndIndex() {
			return endIndex;
		}

		public void setEndIndex(int endIndex) {
			this.endIndex = endIndex;
		}
	}
}
